/*
 * @Author: 檃
 * @Date: 2022-07-05 14:35:13
 * @LastEditTime: 2023-01-05 11:25:48
 * @Description:
 * @LastEditors: 小胖沱 1643200914@qq.com
 */
import { message } from 'antd'
import { useEffect, useState } from 'react'

export default () => {
  const [isFull, setIsFull] = useState(false) // 强制渲染

  useEffect(() => {
    document.addEventListener(
      'keydown',
      async function (e) {
        if (e.keyCode == 13) {
          try {
            await toggleFullScreen()
          } catch (err) {
            console.log(err)
          }
        }
      },
      false
    )
  }, [])

  const toggleFullScreen = () => {
    try {
      if (!document.fullscreenElement) {
        document.documentElement.requestFullscreen()
        setIsFull(true)
      } else {
        if (document.exitFullscreen) {
          document.exitFullscreen()
          setIsFull(false)
        }
      }
    } catch (err) {
      console.log(err)
    }
  }

  useEffect(() => {
    message.success('是否全屏:' + `${isFull}`)
  }, [])

  return { isFull, toggleFullScreen }
}
